<?php
declare (strict_types = 1);

namespace app\paths\controller;

use app\common\model\admin\AccountModel;
use app\common\model\paths\ArticleModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;
use think\admin\service\AdminService;
use think\facade\Validate;

/**
 * 文章管理
 * @package app\paths\controller
 * @author wangqinghui
 * @DateTime 2024-09-25 17:04:21
 */
class Article extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @author wangqinghui
     * @DateTime 2024-09-25 17:04:51
     * @return void
     */
    public function index()
    {
        ArticleModel::mQuery()->layTable(function () {
            $this->title = '文章管理';
            // 获取类型
            $this->typeArr = ArticleModel::getTypeArr();
            // 获取状态
            $this->statusArr = ArticleModel::getStatusArr();
            // 获取下级数据
           // $this->checkAuth(AdminService::getPlatformId());
        }, function (QueryHelper $query) {
            $query->like('title')->equal('status,type,account_id')->dateBetween('create_at,publish_time');
            $query->order('sort desc,id asc');
        });
    }

    /**
     * 格式化列表数据
     * @author wangqinghui
     * @DateTime 2024-09-25 17:06:00
     * @param array $data
     * @return void
     */
    protected function _index_page_filter(array &$data)
    {
        if(!empty($data)){

            $typeArr       = ArticleModel::getTypeArr();
            $account_ids   = array_column($data, 'account_id');
            $account_data  = [];

            if(!empty($account_ids)) {
                $account_data = AccountModel::mk()->where('id', 'in', $account_ids)->column('title', 'id');
            }

            foreach ($data as $key => $value) {
                $data[$key]['type_text'] = $typeArr[$value['type']] ?? '';
                $data[$key]['platform_text'] = $account_data[$value['account_id']] ?? '';
            }
        }
    }

    /**
     * 添加
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-25 17:09:56
     * @return void
     */
    public function add()
    {
        ArticleModel::mForm('form');
    }

    /**
     * 编辑
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-25 17:10:11
     * @return void
     */
    public function edit()
    {
        ArticleModel::mForm('form');
    }

    /**
     * 处理提交数据
     * @author wangqinghui
     * @DateTime 2024-09-25 17:07:55
     * @param array $data
     * @return void
     */
    protected function _form_filter(array &$data)
    {

        if($this->request->isPost()){
            $data['account_id'] = $data['account_id'] ?? $this->account_id;
            $data['user_id']     = $data['user_id']     ?? AdminService::getUserId();
            $rule = [
                'type|类型' => 'require|in:1,2',
                'title|标题' => 'require|length:1,250',
                'content|内容' => 'require|length:1,65535',
                'cover|封面图' => 'require|length:1,255',
                'intro|简介'    => 'require|length:1,500',
                'account_id|所属学校'  => 'require|number',
                'user_id|老师'  => 'require|number',
            ];
            $validate = Validate::rule($rule);

            if(!$validate->check($data)){
                $this->error($validate->getError());
            }

            //$data['user_id']    = $data['user_id'] ?? $this->adminUser['id'];
            //$data['account_id'] = $data['account_id'] ?? $this->account_id;
        }else{
            $this->typeArr = ArticleModel::getTypeArr();
            //$account = $data['account_id'] ?? $this->account_id;
           // $this->teacher_arr   = SystemUser::mk()->where('account_id', $account)->where('usertype', 'teacher')->column('nickname', 'id');
        }
    }

    /**
     * 更新状态
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-25 17:10:35
     * @return void
     */
    public function state()
    {
        $id = $this->request->post('id', 0);
        if(empty($id) || ($id <= 0)){
            $this->error('参数错误');
        }
        $data = $this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]);
        if($data['status'] == 1){
            $data['publish_time'] = date('Y-m-d H:i:s');
        }else{
            $data['publish_time'] = '0000-00-00 00:00:00';
        }
        ArticleModel::mSave($data, 'id', [
            'id'    => $id
        ]);
    }

    /**
     * 删除
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-25 17:15:29
     * @return void
     */
    public function remove ()
    {
        $id = $this->request->post('id', 0);
        if(empty($id) || ($id <= 0)){
            $this->error('参数错误');
        }
        ArticleModel::mDelete('id', [
            'id'    => $id
        ]);
    }
}